package com.hhkj.admin.train.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.factroy.AsyncTrainAnswerFactory;
import com.hhkj.admin.train.model.dopo.TrainDo;
import com.hhkj.admin.train.model.dopo.TrainPo;
import com.hhkj.admin.train.model.req.TrainReq;
import com.hhkj.admin.train.model.res.TrainRes;
import com.hhkj.admin.train.service.base.TrainService;
import com.hhkj.admin.train.service.biz.TrainBizService;
import com.hhkj.admin.train.service.extend.TrainExtendService;
import com.hhkj.api.common.session.holder.ApiStudentSessionHolder;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.framework.manager.AsyncManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 培训练习Service接口
 *
 * @author ruoyi
 * @since 2023-06-08
 */
@Service
public class TrainBizServiceImpl
        extends BaseBizServiceImpl<Train, TrainReq, TrainRes, TrainDo, TrainPo, TrainService, TrainExtendService>
        implements TrainBizService {
    @Autowired
    private TrainService trainService;

    @Override
    public TrainPo getPoById(Long id) {
        return trainService.getPoById(id);
    }

    /**
     * 查询培训练习
     *
     * @param id 培训练习主键
     * @return 培训练习
     */
    @Override
    public Train selectTrainById(Long id) {
        return trainService.getById(id);
    }

    /**
     * 获取详情方法
     *
     * @param id 数据id
     * @return
     */
    @Override
    public TrainRes getById(Long id) {
        Train train = this.selectTrainById(id);
        TrainRes res = new TrainRes();
        BeanUtil.copyProperties(train, res);
        return res;
    }

    /**
     * 查询培训练习列表
     *
     * @param train 培训练习
     * @return 培训练习
     */
    @Override
    public List<Train> selectTrainList(Train train) {
        return trainService.selectTrainList(train);
    }

    /**
     * 新增培训练习
     *
     * @param trainReq 培训练习
     * @return 结果
     */
    @Override
    public Integer insertTrain(TrainReq trainReq) {
        //保存主表信息
        applyTrainInfo(trainReq);
        int saveFlag = trainService.getBaseMapper().insert(trainReq);
        //异步保存数据
        AsyncManager.me().execute(AsyncTrainAnswerFactory.asyncSaveStudentAnswer(trainReq, trainReq.getAnswerList()));
        return saveFlag;
    }

    /**
     * 处理培训主表信息
     *
     * @param trainReq 主表数据
     */
    private void applyTrainInfo(TrainReq trainReq) {
        trainReq.setCreateBy(ApiStudentSessionHolder.getStudentId());
        trainReq.setCreateTime(new Date());
    }

    /**
     * 修改培训练习
     *
     * @param trainReq 培训练习
     * @return 结果
     */
    @Override
    public boolean updateTrain(TrainReq trainReq) {
        this.beforeUpdate(trainReq);
        boolean result = trainService.updateById(trainReq);
        if (result) {
            this.afterUpdate(trainReq);
        }
        return result;
    }

    /**
     * 修改前置
     *
     * @param trainReq 培训信息
     */
    public void beforeUpdate(TrainReq trainReq) {
        trainReq.setUpdateBy(ApiStudentSessionHolder.getStudentId());
        trainReq.setUpdateTime(new Date());
        trainReq.setDisableUpdateByFill(true);
    }


    /**
     * 修改后置方法
     *
     * @param trainReq 培训信息
     */
    public void afterUpdate(TrainReq trainReq) {
        //异步保存数据
        AsyncManager.me().execute(AsyncTrainAnswerFactory.asyncUpdateStudentAnswer(trainReq));
    }

    @Override
    public PageInfo<TrainRes> paging(TrainReq trainReq) {
        //查询
        List<Train> trainList = this.selectTrainList(trainReq);
        //转换
        List<TrainRes> resList = BeanUtil.copyToList(trainList, resClass);
        PageInfo<TrainRes> resPageInfo = new PageInfo<>(resList);
        BeanUtil.copyProperties(trainList, resPageInfo);
        return resPageInfo;
    }

    @Override
    public boolean save(TrainReq trainReq) {
        Integer integer = this.insertTrain(trainReq);
        return integer > 0;
    }

    /**
     * 修改方法
     *
     * @param trainReq train
     * @return
     */
    @Override
    public boolean update(TrainReq trainReq) {
        return this.updateTrain(trainReq);
    }
}
